<template>
	<view class="person-location-layout">
		<view class="bgview">
			<view class="hint">
				{{$t('messageNotify.content')}}
			</view>
			<view class="right" @click="toOpenSetting">
				<view class="persimion" :class=" !isNotifyAllowed?'active':''">
					{{isNotifyAllowed?$t('messageNotify.allow'):$t('messageNotify.nonePersimion')}}
				</view>
				<uni-icons type="right"></uni-icons>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue';
	import {
		onLoad,
		onShow
	} from "@dcloudio/uni-app";
	import {
		i18nTool
	} from '../../utils/i18en';
	const isNotifyAllowed = ref(false);


	onShow(() => {
		getNotifyPermision();
	})
	onLoad(() => {
		uni.setNavigationBarTitle({
			title: i18nTool('messageNotify.navTitle')
		})
	})

	function toOpenSetting() {
		// #ifdef APP-PLUS
		if (plus.os.name == 'Android') { // 判断是Android
			var main = plus.android.runtimeMainActivity();
			var pkName = main.getPackageName();
			var uid = main.getApplicationInfo().plusGetAttribute("uid");

			var Intent = plus.android.importClass('android.content.Intent');
			var Build = plus.android.importClass("android.os.Build");
			//android 8.0引导  
			if (Build.VERSION.SDK_INT >= 26) {
				var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
				intent.putExtra('android.provider.extra.APP_PACKAGE', pkName);
			} else if (Build.VERSION.SDK_INT >= 21) { //android 5.0-7.0  
				var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
				intent.putExtra("app_package", pkName);
				intent.putExtra("app_uid", uid);
			} else { //(<21)其他--跳转到该应用管理的详情页  
				intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
				var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
				intent.setData(uri);
			}
			// 跳转到该应用的系统通知设置页  
			main.startActivity(intent);
		} else if (plus.os.name == 'iOS') { // 判断是iOS
			var app = plus.ios.invoke('UIApplication', 'sharedApplication');
			var setting = plus.ios.invoke('NSURL', 'URLWithString:', 'app-settings:');
			plus.ios.invoke(app, 'openURL:', setting);
			plus.ios.deleteObject(setting);
			plus.ios.deleteObject(app);
		}
		// #endif  
	}

	function getNotifyPermision() {
		// #ifdef APP-PLUS
		if (plus.os.name == 'Android') { // 判断是Android
			var main = plus.android.runtimeMainActivity();
			var pkName = main.getPackageName();
			var uid = main.getApplicationInfo().plusGetAttribute("uid");
			var NotificationManagerCompat = plus.android.importClass("android.support.v4.app.NotificationManagerCompat");
			//android.support.v4升级为androidx
			if (NotificationManagerCompat == null) {
				NotificationManagerCompat = plus.android.importClass("androidx.core.app.NotificationManagerCompat");
			}
			var areNotificationsEnabled = NotificationManagerCompat.from(main).areNotificationsEnabled();
			isNotifyAllowed.value = areNotificationsEnabled;

		} else if (plus.os.name == 'iOS') { // 判断是iOS
			var isOn = undefined;
			var types = 0;
			var app = plus.ios.invoke('UIApplication', 'sharedApplication');
			var settings = plus.ios.invoke(app, 'currentUserNotificationSettings');
			if (settings) {
				types = settings.plusGetAttribute('types');
				plus.ios.deleteObject(settings);
			} else {
				types = plus.ios.invoke(app, 'enabledRemoteNotificationTypes');
			}
			plus.ios.deleteObject(app);
			isOn = (0 != types);
			isNotifyAllowed.value = isOn;
		}
		// #endif  
	}
</script>

<style lang="scss" scoped>
	.person-location-layout {
		margin: 0rpx 40rpx;

		.bgview {

			height: 100rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			border-bottom: 2rpx solid #f1f6f0;

			.right {
				display: flex;
				justify-content: start;
				align-items: center;

				.persimion {
					color: #999;
					font-size: 26rpx;

					&.active {
						color: #53786C;
					}
				}

			}
		}

	}
</style>